<template>
  <div class="user-profile">
    <van-nav-bar
      class="login-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    />

    <input type="file" hidden ref="inputFile" @change="inputChange">
    <van-cell title="头像" is-link @click="$refs.inputFile.click()">
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="user.photo"
      />
    </van-cell>
    <van-cell title="昵称" @click="isShowUpdateName = true" :value="user.name" is-link />
    <van-cell title="性别" @click="isShowUpdateGender = true" :value="user.gender ? '女' : '男'" is-link />
    <van-cell title="生日" @click="isShowUpdateBirthday = true" :value="user.birthday || '1977-00-00'" is-link />

    <van-popup
      v-model="isShowUpdateName"
      style="height: 100%"
      position="bottom">
      <update-name
        v-if="isShowUpdateName"
        v-model="user.name"
        @close="isShowUpdateName = false"
      />
    </van-popup>

    <van-popup
      v-model="isShowUpdateGender"
      position="bottom">
      <update-gender
        v-model="user.gender"
        @close="isShowUpdateGender = false"
      />
    </van-popup>

    <van-popup
      v-model="isShowUpdateBirthday"
      position="bottom">
        <update-birthday
          v-model="user.birthday"
          @close="isShowUpdateBirthday = false"
        />
    </van-popup>

    <van-popup
      v-model="isShowUpdateAvatar"
      style="height: 100%"
      position="bottom">
        <update-avatar
          :src="url"
          @update-photo="user.photo = $event"
          @close="isShowUpdateAvatar = false"
        />
    </van-popup>
  </div>
</template>

<script>
import {getUserProfile} from '@/api/user'
import UpdateName from './components/update-name'
import UpdateGender from './components/update-gender'
import UpdateBirthday from './components/update-birthday'
import UpdateAvatar from './components/update-avatar'

export default {
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdateAvatar
  },
  created() {
    this.getUserProfileFn()
  },
  data() {
    return {
      user: {},
      isShowUpdateName: false,
      isShowUpdateGender: false,
      isShowUpdateBirthday: false,
      isShowUpdateAvatar: false,
      url: ''
    }
  },
  methods: {
    async getUserProfileFn() {
      try {
        const {data: {data}} = await getUserProfile()

        this.user = data
      } catch(err) {
        console.log(err)
      }
    },
    inputChange() {
      let file = this.$refs.inputFile.files[0]

      this.url = window.URL.createObjectURL(file)

      this.isShowUpdateAvatar = true

      this.$refs.inputFile.value = ''
    }
  }
}
</script>

<style scoped lang='less'>
.user-profile {
  .avatar {
    width: 60px;
    height: 60px;
  }
}
</style>
